package com.community.body.service.impl;

import java.util.List;


import com.community.body.exception.CustomException;
import com.community.body.domain.ZyBuilding;
import com.community.body.mapper.ZyBuildingMapper;
import com.community.body.service.IZyBuildingService;
import com.community.common.utils.DateUtils;
import com.community.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 楼栋信息Service业务层处理
 *
 * @author community
 * @date 2024-04-09
 */
@Service
public class ZyBuildingServiceImpl implements IZyBuildingService {
    @Autowired
    private ZyBuildingMapper zyBuildingMapper;

    private static final Logger log = LoggerFactory.getLogger(ZyBuildingServiceImpl.class);

    /**
     * 查询楼栋信息
     *
     * @param buildingId 楼栋信息主键
     * @return 楼栋信息
     */
    @Override
    public ZyBuilding selectZyBuildingByBuildingId(Long buildingId) {

        return zyBuildingMapper.selectZyBuildingByBuildingId(buildingId);
    }

    /**
     * 查询楼栋信息列表
     *
     * @param zyBuilding 楼栋信息
     * @return 楼栋信息
     */
    @Override
    public List<ZyBuilding> selectZyBuildingList(ZyBuilding zyBuilding) {
        return zyBuildingMapper.selectZyBuildingList(zyBuilding);
    }


    /**
     * 部分导入
     *
     * @param longArray
     * @return
     */
    @Override
    public List<ZyBuilding> selectZyBuildingExport(Long[] longArray) {
        return zyBuildingMapper.selectZyBuildingByArray(longArray);
    }

    /**
     * 新增楼栋信息
     *
     * @param zyBuilding 楼栋信息
     * @return 结果
     */
    @Override
    public int insertZyBuilding(ZyBuilding zyBuilding) {
        zyBuilding.setCreateTime(DateUtils.getNowDate());
        long l = System.currentTimeMillis();
        zyBuilding.setBuildingCode("Building_" + l);
        return zyBuildingMapper.insertZyBuilding(zyBuilding);
    }

    /**
     * 修改楼栋信息
     *
     * @param zyBuilding 楼栋信息
     * @return 结果
     */
    @Override
    public int updateZyBuilding(ZyBuilding zyBuilding) {
        zyBuilding.setUpdateTime(DateUtils.getNowDate());
        return zyBuildingMapper.updateZyBuilding(zyBuilding);
    }

    /**
     * 批量删除楼栋信息
     *
     * @param buildingIds 需要删除的楼栋信息主键
     * @return 结果
     */
    @Override
    public int deleteZyBuildingByBuildingIds(Long[] buildingIds) {
        return zyBuildingMapper.deleteZyBuildingByBuildingIds(buildingIds);
    }

    /**
     * 删除楼栋信息信息
     *
     * @param buildingId 楼栋信息主键
     * @return 结果
     */
    @Override
    public int deleteZyBuildingByBuildingId(Long buildingId) {
        return zyBuildingMapper.deleteZyBuildingByBuildingId(buildingId);
    }

    /**
     * 导入
     *
     * @param dataManagementList
     * @param isUpdateSupport
     * @return
     */
    @Override
    public String importData(List<ZyBuilding> dataManagementList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(dataManagementList) || dataManagementList.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<ZyBuilding> existList = selectZyBuildingList(null);
        for (ZyBuilding importData : dataManagementList) {
            try {
                //验证是否存在这个信息
                boolean userFlag = false;
                for (ZyBuilding entry : existList) {
                    if (entry.getCommunityId().equals(importData.getCommunityId()) && entry.getBuildingName().equals(importData.getBuildingName())) {
                        userFlag = true;
                        break;
                    }
                }
                if (!userFlag) {
                    insertZyBuilding(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getBuildingName() + " 导入成功");
                } else if (isUpdateSupport) {
                    updateZyBuilding(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getBuildingName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + importData.getBuildingName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据 " + importData.getBuildingName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
